package com.lonely.base;

import org.junit.Before;
import org.junit.Test;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @date: 2022-11-28 09:38
 * @author: ztkj-hzb
 * @description: 通过配置json配置文件的方式
 **/
public class CsvSchemaTest {


    private Connection conn;

    @Before
    public void setup() throws SQLException {
        Properties config = new Properties();
        config.put("model", CsvSchemaTest.class.getClassLoader().getResource("csv_default.json").getPath());

        //设置不区分sql大小写
        config.put("caseSensitive", "false");
        conn = DriverManager.getConnection("jdbc:calcite:",config);
    }

    @Test
    public void query() throws Exception {
        List<String> sqlList = new ArrayList<>();
        sqlList.add("select * from csv.csv_user");
        sqlList.add("select id, name || '_after_append' from csv_user");
        //sqlList.add("select id, concat(name,'_after_append') from csv_user");
        sqlList.add("select t.id,t.name,t2.age from csv_user t left join csv_detail t2 on t.id = t2.id");
        for (String sql : sqlList) {
            System.out.println("-----------------");
            System.out.println(sql);
            printResultSet(conn.createStatement().executeQuery(sql));
        }
    }

    private void printResultSet(ResultSet resultSet) throws SQLException {
        ResultSetMetaData metaData = resultSet.getMetaData();
        int columnCount = metaData.getColumnCount();
        while(resultSet.next()){
            List<Object> row = new ArrayList<>();
            for (int i = 1; i < columnCount+1; i++) {
                row.add(resultSet.getObject(i));
            }
            System.out.println(row);
        }
    }

}
